package xframe.util.tld;

import java.io.IOException;
import java.util.HashMap;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.TagSupport;


import xframe.util.UtilMaster;
import xframe.util.db.Row;

public class Paging extends TagSupport{


    private int page;
    private int pageCnt;
    private int totalCnt;
    private int beginPage;
    private int listSize;
    private int pageSize;
    private int totalPageCnt;
    private int startPage;
    private int endPage;
    private String link;
    private	String prefix;
    
    public Paging()    {
        page = 0;
        pageCnt = 0;
        totalCnt = 0;
        beginPage = 0;
        listSize = 0;
        pageSize = 0;
        totalPageCnt = 0;
        startPage = 0;
        endPage = 0;
        link = "";
    }

    public void setLink(String link)
    {
        this.link = link;
    }

    public void setPage(int page)
    {
        this.page = page;
    }

    public void setTotalCnt(int totalCnt)
    {
        this.totalCnt = totalCnt;
    }

    public void setListCnt(int listCnt)
    {
        listSize = listCnt;
    }

    public void setPageSize(int pageSize)
    {
        this.pageSize = pageSize;
    }
    
    public int getPageSize()
    {
        return pageSize;
    }

    public void setListSize(int listSize)
    {
        this.listSize = listSize;
    }    


	public String getPrefix() {
		return prefix;
	}

	public void setPrefix(String prefix) {
		this.prefix = prefix;
	}
	
	public int doStartTag()throws JspException{
        try{
            HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
            HttpServletResponse response = (HttpServletResponse)pageContext.getResponse();
            JspWriter out = pageContext.getOut();
            StringBuffer pageList = new StringBuffer();
            StringBuffer parameterList = new StringBuffer();
            setTotalCnt(totalCnt);
            setPage(page);
            setListCnt(listSize);
            setPrefix(prefix);
            pageCnt = totalCnt / listSize;
            if(totalCnt % listSize != 0)
                pageCnt++;
            totalPageCnt = pageCnt;
            if(pageSize < pageCnt){
                pageCnt = pageSize;
            }
            
            if(totalPageCnt - page / pageSize > 0){
                beginPage = page;
            }else{
                beginPage = 1;
            }
            
            Row requestMap = (Row)request.getAttribute("request");
            
            if(requestMap.get("page") != null){
                page = Integer.parseInt((String)requestMap.get("page"));
            }else{
                page = 1;
            }
            
            requestMap.remove("page");
            
            if(prefix != null && prefix.length() > 0){
            	parameterList.append(prefix);
            }
            parameterList.append(request.getContextPath());
            parameterList.append("?");
            String requestIdex[] = UtilMaster.getHashKeys(requestMap);
            Vector v_temp = requestMap.getTempKeys();
            
            for(int i = 0; i < requestIdex.length; i++){
                try {
                	if(!v_temp.contains(requestIdex[i])){
					parameterList.append(requestIdex[i] + "=" + requestMap.getString(requestIdex[i]) + "&");
                	}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
               // System.out.println(requestIdex[i] + "=" + requestMap.get(requestIdex[i]) + "&");
            }
            int times = page / pageSize;
            int poz = page % pageSize;
            if(poz == 0){
                beginPage = (times * pageSize - pageSize) + 1;
            }else{
                beginPage = times * pageSize + 1;
            }
            
            int endPage = 0;
            endPage = (beginPage + pageSize) - 1;
            if(totalPageCnt < endPage){
                endPage = totalPageCnt;
            }
            
            pageList.append("<div class='paging'>");
            if(page > pageSize)
                pageList.append("<a href = \"" + parameterList + "page=" + (beginPage - 1) + "\">" + "<<</a>");
            for(int i = beginPage; i < endPage + 1; i++)
                if(page == i){
                    pageList.append("<strong>" + i + "</strong>");
                }else{
                    link = " <a href = \"" + parameterList + "page=" + i + "\">";
                    pageList.append(link + i);
                    pageList.append("</a>");
                }

            if(totalPageCnt - beginPage - pageSize >= 0){
                pageList.append("<a href = \"" + parameterList + "page=" + (beginPage + pageCnt) + "\">");
                pageList.append(">>");
                pageList.append("</a>");
            }
            pageList.append("</div>");
            out.print(pageList);
            
        }catch(IOException ioe){
            ioe.printStackTrace();
        }
        return 0;
    }




}
